//
//  TXHRulerScrollView.m
//  PrettyRuler
//
//  Created by GXY on 15/12/11.
//  Copyright © 2015年 Tangxianhai. All rights reserved.
//

#import "TXHRulerScrollViewOM.h"

@implementation TXHRulerScrollViewOM


- (void)drawRuler {

    CGMutablePathRef pathRef1 = CGPathCreateMutable();
    CGMutablePathRef pathRef2 = CGPathCreateMutable();
    
    CAShapeLayer *shapeLayer1 = [CAShapeLayer layer];
    shapeLayer1.strokeColor = [UIColor lightGrayColor].CGColor;
    shapeLayer1.fillColor = [UIColor clearColor].CGColor;
    shapeLayer1.lineWidth = 1.f;
    shapeLayer1.lineCap = kCALineCapButt;
    
    CAShapeLayer *shapeLayer2 = [CAShapeLayer layer];
    shapeLayer2.strokeColor = [UIColor grayColor].CGColor;
    shapeLayer2.fillColor = [UIColor clearColor].CGColor;
    shapeLayer2.lineWidth = 1.f;
    shapeLayer2.lineCap = kCALineCapButt;
    
    
    int index;
    for (int i = self.rulerMixWeight ; i <= self.rulerMaxWeight ; i++) {
        UILabel *rule = [[UILabel alloc] init];
        rule.textColor = [UIColor blackColor];
        rule.text = [NSString stringWithFormat:@"%d",i];
        CGSize textSize = [rule.text sizeWithAttributes:@{ NSFontAttributeName : rule.font }];
        index = (i - self.rulerMixWeight);
        if (i % 10 == 0) {
            CGPathMoveToPoint(pathRef2, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index , DISTANCETOPANDBOTTOM);  //从下往上
            CGPathAddLineToPoint(pathRef2, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index, self.rulerHeight - DISTANCETOPANDBOTTOM - textSize.height-40);
            rule.frame = CGRectMake(DISTANCELEFTANDRIGHT + DISTANCEVALUE * index - textSize.width / 2, self.rulerHeight - DISTANCETOPANDBOTTOM - textSize.height+10, 0, 0);//数字 的位置
            [rule sizeToFit];
            [self addSubview:rule];
        }
        else if (i % 5 == 0) {
            CGPathMoveToPoint(pathRef1, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index , DISTANCETOPANDBOTTOM -5);
            CGPathAddLineToPoint(pathRef1, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index, self.rulerHeight - DISTANCETOPANDBOTTOM - textSize.height - 10-20-20);
        }
        else
        {
            CGPathMoveToPoint(pathRef1, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index , DISTANCETOPANDBOTTOM + 10-20);
            CGPathAddLineToPoint(pathRef1, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index, self.rulerHeight - DISTANCETOPANDBOTTOM - textSize.height - 20-30);
        }
    }
    
    shapeLayer1.path = pathRef1;
    shapeLayer2.path = pathRef2;
    [self.layer addSublayer:shapeLayer1];
    [self.layer addSublayer:shapeLayer2];
    self.frame = CGRectMake(0, 0, self.rulerWidth, self.rulerHeight);

    UIEdgeInsets edge = UIEdgeInsetsMake(0, self.rulerWidth / 2.f - DISTANCELEFTANDRIGHT, 0, self.rulerWidth / 2.f - DISTANCELEFTANDRIGHT);
    self.contentInset = edge;
    self.contentOffset = CGPointMake(DISTANCEVALUE * (self.rulerValue-self.rulerMixWeight) - self.rulerWidth / 2.f + DISTANCELEFTANDRIGHT, 0);
    self.contentSize = CGSizeMake((self.rulerMaxWeight - self.rulerMixWeight) * DISTANCEVALUE + DISTANCELEFTANDRIGHT * 2.f, self.rulerHeight);
}

- (void)drawRulerUp {
    
    CGMutablePathRef pathRef1 = CGPathCreateMutable();
    CGMutablePathRef pathRef2 = CGPathCreateMutable();
    
    CAShapeLayer *shapeLayer1 = [CAShapeLayer layer];
    shapeLayer1.strokeColor = [UIColor lightGrayColor].CGColor;
    shapeLayer1.fillColor = [UIColor clearColor].CGColor;
    shapeLayer1.lineWidth = 1.f;
    shapeLayer1.lineCap = kCALineCapButt;
    
    CAShapeLayer *shapeLayer2 = [CAShapeLayer layer];
    shapeLayer2.strokeColor = [UIColor grayColor].CGColor;
    shapeLayer2.fillColor = [UIColor clearColor].CGColor;
    shapeLayer2.lineWidth = 1.f;
    shapeLayer2.lineCap = kCALineCapButt;

    int index;
    for (int i = self.rulerMixWeight ; i <= self.rulerMaxWeight ; i++) {
        UILabel *rule = [[UILabel alloc] init];
        rule.textColor = [UIColor blackColor];
        rule.text = [NSString stringWithFormat:@"%d",i];
        CGSize textSize = [rule.text sizeWithAttributes:@{ NSFontAttributeName : rule.font}];
        index = (i - self.rulerMixWeight);
        if (i % 10 == 0) {
            CGPathMoveToPoint(pathRef2, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index , DISTANCETOPANDBOTTOM+10);  //从下往上
            CGPathAddLineToPoint(pathRef2, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index, self.rulerHeight - DISTANCETOPANDBOTTOM - textSize.height+100);
            rule.frame = CGRectMake(DISTANCELEFTANDRIGHT + DISTANCEVALUE * index - textSize.width / 2, self.rulerHeight - DISTANCETOPANDBOTTOM - textSize.height, 0, 0);
            [rule sizeToFit];
            [self addSubview:rule];
        }
        else if (i % 5 == 0) {
            CGPathMoveToPoint(pathRef1, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index , DISTANCETOPANDBOTTOM + 5+10);
            CGPathAddLineToPoint(pathRef1, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index, self.rulerHeight - DISTANCETOPANDBOTTOM - textSize.height - 10-20+100);
        }
        else
        {
            CGPathMoveToPoint(pathRef1, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index , DISTANCETOPANDBOTTOM+20);
            CGPathAddLineToPoint(pathRef1, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index, self.rulerHeight - DISTANCETOPANDBOTTOM - textSize.height - 20+100);
        }
    }
    shapeLayer1.path = pathRef1;
    shapeLayer2.path = pathRef2;
    [self.layer addSublayer:shapeLayer1];
    [self.layer addSublayer:shapeLayer2];
    self.frame = CGRectMake(0, 0, self.rulerWidth, self.rulerHeight);
    
    UIEdgeInsets edge = UIEdgeInsetsMake(0, self.rulerWidth / 2.f - DISTANCELEFTANDRIGHT, 0, self.rulerWidth / 2.f - DISTANCELEFTANDRIGHT);
    self.contentInset = edge;
    self.contentOffset = CGPointMake(DISTANCEVALUE * (self.rulerValue-self.rulerMixWeight) - self.rulerWidth / 2.f + DISTANCELEFTANDRIGHT, 0);
    self.contentSize = CGSizeMake((self.rulerMaxWeight - self.rulerMixWeight) * DISTANCEVALUE + DISTANCELEFTANDRIGHT * 2.f, self.rulerHeight);
}

//<<<<<<< .mine
//
//=======
// */
//- (void)drawRuler {
//    
//    CGMutablePathRef pathRef1 = CGPathCreateMutable();
//    CGMutablePathRef pathRef2 = CGPathCreateMutable();
//    
//    CAShapeLayer *shapeLayer1 = [CAShapeLayer layer];
//    shapeLayer1.strokeColor = [UIColor lightGrayColor].CGColor;
//    shapeLayer1.fillColor = [UIColor clearColor].CGColor;
//    shapeLayer1.lineWidth = 1.f;
//    shapeLayer1.lineCap = kCALineCapButt;
//    
//    CAShapeLayer *shapeLayer2 = [CAShapeLayer layer];
//    shapeLayer2.strokeColor = [UIColor grayColor].CGColor;
//    shapeLayer2.fillColor = [UIColor clearColor].CGColor;
//    shapeLayer2.lineWidth = 1.f;
//    shapeLayer2.lineCap = kCALineCapButt;
//    
//    int index;
//    for (int i = self.rulerMixWeight ; i <= self.rulerMaxWeight ; i++) {
//        UILabel *rule = [[UILabel alloc] init];
//        rule.textColor = [UIColor blackColor];
//        rule.text = [NSString stringWithFormat:@"%d",i];
//        CGSize textSize = [rule.text sizeWithAttributes:@{ NSFontAttributeName : rule.font }];
//        index = (i - self.rulerMixWeight);
//        if (i % 10 == 0) {
//            CGPathMoveToPoint(pathRef2, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index , DISTANCETOPANDBOTTOM);  //从下往上
//            CGPathAddLineToPoint(pathRef2, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index, self.rulerHeight - DISTANCETOPANDBOTTOM - textSize.height-40);
//            rule.frame = CGRectMake(DISTANCELEFTANDRIGHT + DISTANCEVALUE * index - textSize.width / 2, self.rulerHeight - DISTANCETOPANDBOTTOM - textSize.height+10, 0, 0);//数字 的位置
//            [rule sizeToFit];
//            [self addSubview:rule];
//        }
//        else if (i % 5 == 0) {
//            CGPathMoveToPoint(pathRef1, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index , DISTANCETOPANDBOTTOM -5);
//            CGPathAddLineToPoint(pathRef1, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index, self.rulerHeight - DISTANCETOPANDBOTTOM - textSize.height - 10-20-20);
//        }
//        else
//        {
//            CGPathMoveToPoint(pathRef1, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index , DISTANCETOPANDBOTTOM + 10-20);
//            CGPathAddLineToPoint(pathRef1, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index, self.rulerHeight - DISTANCETOPANDBOTTOM - textSize.height - 20-30);
//        }
//        
//        
//    }
//    
//    shapeLayer1.path = pathRef1;
//    shapeLayer2.path = pathRef2;
//    [self.layer addSublayer:shapeLayer1];
//    [self.layer addSublayer:shapeLayer2];
//    self.frame = CGRectMake(0, 0, self.rulerWidth, self.rulerHeight);
//    
//    UIEdgeInsets edge = UIEdgeInsetsMake(0, self.rulerWidth / 2.f - DISTANCELEFTANDRIGHT, 0, self.rulerWidth / 2.f - DISTANCELEFTANDRIGHT);
//    self.contentInset = edge;
//    self.contentOffset = CGPointMake(DISTANCEVALUE * (self.rulerValue-self.rulerMixWeight) - self.rulerWidth / 2.f + DISTANCELEFTANDRIGHT, 0);
//    self.contentSize = CGSizeMake((self.rulerMaxWeight - self.rulerMixWeight) * DISTANCEVALUE + DISTANCELEFTANDRIGHT * 2.f, self.rulerHeight);
//}
//
//- (void)drawRulerUp {
//    
//    CGMutablePathRef pathRef1 = CGPathCreateMutable();
//    CGMutablePathRef pathRef2 = CGPathCreateMutable();
//    
//    CAShapeLayer *shapeLayer1 = [CAShapeLayer layer];
//    shapeLayer1.strokeColor = [UIColor lightGrayColor].CGColor;
//    shapeLayer1.fillColor = [UIColor clearColor].CGColor;
//    shapeLayer1.lineWidth = 1.f;
//    shapeLayer1.lineCap = kCALineCapButt;
//    
//    CAShapeLayer *shapeLayer2 = [CAShapeLayer layer];
//    shapeLayer2.strokeColor = [UIColor grayColor].CGColor;
//    shapeLayer2.fillColor = [UIColor clearColor].CGColor;
//    shapeLayer2.lineWidth = 1.f;
//    shapeLayer2.lineCap = kCALineCapButt;
//    
//    int index;
//    for (int i = self.rulerMixWeight ; i <= self.rulerMaxWeight ; i++) {
//        UILabel *rule = [[UILabel alloc] init];
//        rule.textColor = [UIColor blackColor];
//        rule.text = [NSString stringWithFormat:@"%d",i];
//        CGSize textSize = [rule.text sizeWithAttributes:@{ NSFontAttributeName : rule.font}];
//        index = (i - self.rulerMixWeight);
//        
//        if (i % 10 == 0) {
//            CGPathMoveToPoint(pathRef2, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index , DISTANCETOPANDBOTTOM+10);  //从下往上
//            CGPathAddLineToPoint(pathRef2, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index, self.rulerHeight - DISTANCETOPANDBOTTOM - textSize.height+100);
//            rule.frame = CGRectMake(DISTANCELEFTANDRIGHT + DISTANCEVALUE * index - textSize.width / 2, self.rulerHeight - DISTANCETOPANDBOTTOM - textSize.height, 0, 0);
//            [rule sizeToFit];
//            [self addSubview:rule];
//        }
//        else if (i % 5 == 0) {
//            CGPathMoveToPoint(pathRef1, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index , DISTANCETOPANDBOTTOM + 5+10);
//            CGPathAddLineToPoint(pathRef1, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index, 0-100);//self.rulerHeight - DISTANCETOPANDBOTTOM - textSize.height - 10-20+100);
//        }
//        else
//        {
//            CGPathMoveToPoint(pathRef1, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index , DISTANCETOPANDBOTTOM+20);
//            CGPathAddLineToPoint(pathRef1, NULL, DISTANCELEFTANDRIGHT + DISTANCEVALUE * index, self.rulerHeight - DISTANCETOPANDBOTTOM - textSize.height - 20+100);
//        }
//        
//        
//        
//        
//    }
//    shapeLayer1.path = pathRef1;
//    shapeLayer2.path = pathRef2;
//    [self.layer addSublayer:shapeLayer1];
//    [self.layer addSublayer:shapeLayer2];
//    self.frame = CGRectMake(0, 0, self.rulerWidth, self.rulerHeight);
//    
//    UIEdgeInsets edge = UIEdgeInsetsMake(0, self.rulerWidth / 2.f - DISTANCELEFTANDRIGHT, 0, self.rulerWidth / 2.f - DISTANCELEFTANDRIGHT);
//    self.contentInset = edge;
//    self.contentOffset = CGPointMake(DISTANCEVALUE * (self.rulerValueUp-self.rulerMixWeight) - self.rulerWidth / 2.f + DISTANCELEFTANDRIGHT, 0);
//    self.contentSize = CGSizeMake((self.rulerMaxWeight - self.rulerMixWeight) * DISTANCEVALUE + DISTANCELEFTANDRIGHT * 2.f, self.rulerHeight);
//}
//
//>>>>>>> .r9148
@end

